package matlap.client.view.student;

import java.util.Iterator;
import java.util.List;

import matlap.client.services.student.StudentService;
import matlap.client.services.student.StudentServiceAsync;
import matlap.shared.Score;
import matlap.shared.Student;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class HierarchyComposite extends Composite {

	StudentServiceAsync ss = GWT.create(StudentService.class);
	private Grid g;
	private int size = 0;
	private VerticalPanel vp;

	public HierarchyComposite() {
		init();

		ss.selectStudentsScore(new AsyncCallback<List<Score>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}

			public void onSuccess(List<Score> result) {
				if (result != null) {
					int i = 1;
					size = result.size() + 1;
					g.resize(size, 2);
					g.setStyleName("NormalGrid");

					Iterator<Score> it = result.iterator();
					while (it.hasNext()) {
						Score stu = (Score) it.next();
						g.setText(i, 0, stu.getName());
						g.setText(i, 1, String.valueOf(stu.getScore()));
						i++;
					}
				}
			}

		});

		ss.selectStudentsWithoutScore(new AsyncCallback<List<Student>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}

			public void onSuccess(List<Student> result) {
				if (result != null) {
					int i = size;
					size += result.size();
					g.resize(size, 2);
					Iterator<Student> it = result.iterator();
					while (it.hasNext()) {
						Student stu = (Student) it.next();
						g.setText(i, 0,
								stu.getLastName() + " " + stu.getFirstName());
						g.setText(i, 1, "0");
						i++;
					}
				}
			}

		});
	}

	private void init() {
		vp = new VerticalPanel();
		initWidget(vp);

		g = new Grid(1, 2);
//		g.setStyleName("Head");

		g.setBorderWidth(2);
	
		Label lb0 = new Label("N\u00E9v");
		lb0.setStyleName("Header");
		g.setWidget(0, 0, lb0);

		Label lb1 = new Label("Pontsz\u00E1m");
		lb1.setStyleName("Header");
		g.setWidget(0, 1, lb1);
		
		vp.add(g);
	}

}
